package cn.itcast.web.controller.cargo;

import cn.itcast.domain.cargo.ExportExample;
import cn.itcast.domain.cargo.ExportProduct;
import cn.itcast.domain.cargo.Packing;
import cn.itcast.domain.cargo.PackingExample;
import cn.itcast.service.cargo.ExportService;
import cn.itcast.service.cargo.PackingService;
import cn.itcast.service.company.CompanyService;
import cn.itcast.vo.PackingListResult;
import cn.itcast.web.controller.BaseController;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/cargo/packing")
public class PackController extends BaseController {
    @Reference
    private PackingService packingService;

    @Reference
    private ExportService exportService;

    @Autowired
    private CompanyService companyService;

    @RequestMapping("/list")
    public String list(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "5")int size,String id){
        PageInfo info = packingService.findAll(page,size,null);
        request.setAttribute("page",info);
        return "cargo/packing/packing-list";
    }

    @RequestMapping("/delete")
    public String delete(String id){
        Packing packing = packingService.findById(id);
        packingService.delete(packing.getId());
        return "redirect:/cargo/packing/list.do";
    }

    @RequestMapping("/toAdd")
    public String toAdd(@RequestParam(defaultValue = "1") int page,@RequestParam(defaultValue = "5") int size){
        ExportExample example = new ExportExample();
        ExportExample.Criteria criteria = example.createCriteria();
        criteria.andStateEqualTo(3l);
        
       PageInfo info =  exportService.findAll(example,page,size);
        request.setAttribute("page",info);
        return "cargo/packing/packing-add";
    }

    @RequestMapping("/edit")
    public String edit(Packing packing,String [] eId){
        String save = packingService.save(packing, eId);
        if (!save.equals("")) {
            request.setAttribute("msg",save);
            return "forward:/error.jsp";
        }
        return "redirect:/cargo/packing/list.do";
    }

    @RequestMapping("/packingList")
    public void downloadExcel(String [] id) throws IOException {
        PackingListResult result = packingService.findById(id);
        String phone = companyService.findPhone(getLoginCompanyId());
        Map<String, Object> map = new HashMap<>();
        map.put("phone",phone);
        map.put("seller", result.getSeller());
        map.put("buyer", result.getBuyer());

        System.out.println(result.getList());

        //2.设置下载信息
        response.setContentType("application/vnd.ms-excel"); //下载excel
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("装箱单", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");

        //3.加载excel模板
        String path = session.getServletContext().getRealPath("/")+"/make/packing.xlsx";
        //4.创建EasyExcel的excelWtire对象( 用于数据填充)
        ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream())
                .head(ExportProduct.class) //设置表头
                .withTemplate(path) //加载模板
                .build();
        //获取sheet对象
        WriteSheet sheet = EasyExcel.writerSheet().build();
        //5.调用方法完成填充map数据
        excelWriter.fill(map,sheet);
        //6.调用方法完成填充list数据
        excelWriter.fill(result.getList(),sheet);
        //7.属性资源，完成下载
        excelWriter.finish(); //下载excel文件，释放内存资源

    }
}
